using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Configuration;
namespace XProject.GUI
{
    public partial class ImageChooser : Form
    {
        DAO.IconDAO daoIcon = new XProject.DAO.IconDAO();
        Entity.FeelingIcon icon = null;
        bool on = false;
        public ImageChooser()
        {
            InitializeComponent();
            icon = new XProject.Entity.FeelingIcon();
        }
        #region properties
        public Entity.FeelingIcon FeelingIcon
        {
            get { return icon; }
            set { icon = value; }
        }
        #endregion
        private void ImageChooser_Load(object sender, EventArgs e)
        {
            IList<Entity.FeelingIcon> icons = daoIcon.GetAll();
            //set icon
           // string ico = Application.StartupPath + "\\" + ConfigurationManager.AppSettings["icon"];
            //if (System.IO.File.Exists(ico)) { this.Icon = new Icon(ico); }
            //load data
            if(icons.Count>0)
            foreach (Entity.FeelingIcon icon in icons)
            {
               // if(File.Exists(icon.ImagePath
                imlEmotions.Images.Add(icon.IconID.ToString(),Image.FromFile(Application.StartupPath+"\\emotions\\"+ icon.ImagePath));
                ListViewItem item = new ListViewItem(icon.Description, icon.IconID.ToString());
                item.Tag = icon.ImagePath;
                lsvImage.Items.Add(item);
            }
            btnMore.ContextMenuStrip = pupMore;
           
        }

        private void btnMore_Click(object sender, EventArgs e)
        { 
            if (!on)
            {
                Point p = new Point(this.Left+btnMore.Left + btnMore.Width + 5, this.Top+btnMore.Top+25);
                btnMore.ContextMenuStrip.Show(p);
            }
            on = !on;
        }

        private void mniAdd_Click(object sender, EventArgs e)
        {
            on = false;
            ImageBrowser imgBrowser = new ImageBrowser();
            if (imgBrowser.ShowDialog() == DialogResult.OK)
            {
              Entity.FeelingIcon icon = new XProject.Entity.FeelingIcon();
               icon.Description = imgBrowser.Description;
               icon.ImagePath = imgBrowser.ImagePath;
               if (daoIcon.Insert(icon) > 0)
               {
                   imlEmotions.Images.Add(icon.IconID.ToString(),Image.FromFile(icon.ImagePath));
                   ListViewItem item = new ListViewItem(icon.Description, icon.IconID.ToString());
                   item.Tag = icon.ImagePath;
                   lsvImage.Items.Add(item);
               }

            }
        }

        private void mniUpdate_Click(object sender, EventArgs e)
        {
            on = false;
            if (lsvImage.SelectedItems.Count == 0)
            {
                MessageBox.Show("Please select a item");
                return;
            }
            Entity.FeelingIcon icon = new XProject.Entity.FeelingIcon(); 
            icon.IconID=int.Parse(lsvImage.SelectedItems[0].ImageKey);
            icon.Description = lsvImage.SelectedItems[0].Text;
            icon.ImagePath = lsvImage.SelectedItems[0].Tag.ToString();
            ImageBrowser dlgImgBrowser = new ImageBrowser();
            dlgImgBrowser.ImagePath = icon.ImagePath;
            dlgImgBrowser.Description = icon.Description;
            if (dlgImgBrowser.ShowDialog() == DialogResult.OK)
            {
                imlEmotions.Images.RemoveByKey(icon.IconID.ToString());
                imlEmotions.Images.Add(icon.IconID.ToString(),Image.FromFile(dlgImgBrowser.ImagePath));
                lsvImage.SelectedItems[0].Tag = dlgImgBrowser.ImagePath;
                icon.ImagePath = dlgImgBrowser.ImagePath;
                daoIcon.Update(icon);
                lsvImage.Update();
            }

        }

        private void mniDelete_Click(object sender, EventArgs e)
        {
            on = false;
            if (lsvImage.SelectedItems.Count == 0)
            {
                MessageBox.Show("Please select an item");                
                return;
            }
            Entity.FeelingIcon icon = new XProject.Entity.FeelingIcon();
            icon.IconID = int.Parse(lsvImage.SelectedItems[0].ImageKey);
            if (lsvImage.SelectedItems[0].Text.ToLower() == "none")
            {
                MessageBox.Show("You cannot delete default item. Action is aborted");
                return;
            }
            if (MessageBox.Show("Are you sure want to remove seletected image?","Confirm",MessageBoxButtons.YesNo,MessageBoxIcon.Question,MessageBoxDefaultButton.Button2) != DialogResult.Yes) return;
            if (daoIcon.Delete(icon) > 0)
            {
                MessageBox.Show("Image has been removed");
                lsvImage.Items.Remove(lsvImage.SelectedItems[0]);
            }
            else MessageBox.Show(daoIcon.Message, "Error");
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (lsvImage.SelectedItems.Count <= 0)
            {
                MessageBox.Show("Please select an item");
                return;
            }

            icon.IconID = int.Parse(lsvImage.SelectedItems[0].ImageKey);
            icon.ImagePath = lsvImage.SelectedItems[0].Tag.ToString();
            this.DialogResult = DialogResult.OK;
        }
    }
}